﻿using System.Threading;

namespace ComTools
{
    public class TimerSleeper 
    {
        System.Threading.Timer TimerSleep = null;
        AutoResetEvent autoResetEvent = new AutoResetEvent(true);

        public void SleepMilliseconds(int milliSeconds)
        {
            if (TimerSleep == null)
            {
                TimerSleep = new System.Threading.Timer(new TimerCallback((obj) =>
                {
                    AutoResetEvent xx = obj as AutoResetEvent;
                    xx.Set();
                }), autoResetEvent, Timeout.Infinite, Timeout.Infinite);
            }

            autoResetEvent.Reset();
            TimerSleep.Change(milliSeconds, Timeout.Infinite);
            autoResetEvent.WaitOne(milliSeconds);
        }

    }
}